﻿using System.Web.Mvc;
using System.Web.Routing;
using Limestone;
using LimestoneMVC.App_Start;
using LimestoneMVC.Controllers;

[assembly: WebActivator.PostApplicationStartMethod(typeof(AppStart_RegisterRoutesAreasFilters), "Start")]

namespace LimestoneMVC.App_Start
{
    public static class AppStart_RegisterRoutesAreasFilters
    {
        public static void Start()
        {
            // Set everything up with you having to do any work.
            // I'm doing this because it means that
            // your app will just run. You might want to get rid of this 
            // and integrate with your own Global.asax. 
            // It's up to you. 
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            // resolve the pagestubcache with autoFac
            var pageStubCache = (IPageStubCache)DependencyResolver.Current.GetService(typeof(IPageStubCache));
            var pageProvider = (IPageProvider)DependencyResolver.Current.GetService(typeof(IPageProvider));

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{resource}.aspx");

            routes.IgnoreRoute("{*staticfile}", new { staticfile = @".*\.(jpg|gif|jpeg|png|js|css|htm|html|htc|flv|swf|ico|bmp|xml)$" });

            // explicitly adding routes as the page catchall route below otherwise will get em all

            // handle pages using the TrialForm template
            routes.MapRoute("trialForm",
                            "{*pagepath}",
                            new { controller = "Trial", action = "ApplyTrial" },
                            new { pagePath = new HandledPagePathConstraint(pageProvider, "TrialForm") }
                );

            // handle pages using the ContactForm template
            routes.MapRoute("contactForm",
                            "{*pagepath}",
                            new { controller = "Contact", action = "GetContact" },
                            new { pagePath = new HandledPagePathConstraint(pageProvider, "ContactForm") }
                );

            // handle pages using the OrderForm template
            routes.MapRoute("orderForm",
                            "{*pagepath}",
                            new { controller = "Order", action = "PlaceOrder" },
                            new { pagePath = new HandledPagePathConstraint(pageProvider, "OrderForm") }
                );

            // all other pages will go to the PageController
            routes.MapRoute("Pages",
                            "{*pagepath}",
                            new { controller = "Page", action = "OpenPage" },
                            new { pagePath = new PagePathConstraint(pageStubCache) }
                );

            // will probably never reach here...
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );



            /*

            routes.MapRoute(
                "SuperCatchall",
                "{*urlParts}",
                new { controller = "Page", action = "OpenPage" }
            );
             */


        }

        /*
         
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.Ignore("{*alllimstonepageextension}",
                            new
                            {
                                allcustomextension = @".*\.page(/.*)?"
                            });

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }
         */
    }

}